import {TripService} from "../../service/trip";
import {CarService} from "../../service/car";
const UNLOCKED = 3

Page({

    carRefresher:0,
    /**
     * 页面的初始数据
     */
    carID: '',
    data: {
        avatarUrl: '',
    },

    onGetUserInfo() {
        wx.getUserProfile({
            desc: '实时展示您到头像',
            success: (res: { userInfo: { avatarUrl: any } }) => {
                this.setData({
                    avatarUrl: res.userInfo.avatarUrl
                })

                wx.setStorageSync("avatarUrl", res.userInfo.avatarUrl)
            },
            fail: () => [
                wx.showToast({
                    icon: "none",
                    title: "获取头像失败"
                })
            ]
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(opt) {
        this.carID = <string>opt.car_id
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const avatarUrl = wx.getStorageSync("avatarUrl")
        if (avatarUrl) {
            this.setData({
                avatarUrl: avatarUrl
            })
        }
    },

    async onUnlock() {
        const trips = await TripService.GetTrips(1)
        if (trips.length != undefined || trips?.trips?.length != undefined) {
            wx.showModal({
                title: "行程提醒",
                content: "当前有未完成的行程",
                success: () => {
                    wx.redirectTo({
                        url: `/pages/driving/driving?trip_id=${trips.trips[0]?.id}`
                    })
                }
            })
            return
        }


        wx.getLocation({
            type: "gcj02",
            success: async loc => {
                console.log("当前地址", {
                    location: {
                        latitude: loc.latitude,
                        longitude: loc.longitude
                    },
                    avatarUrl: this.data.avatarUrl ? this.data.avatarUrl : '',
                    carID: "2124544545"
                })
                try {
                    await wx.showLoading({
                        title: "开锁中",
                        mask: true
                    })

                    if (!this.carID) {
                        await wx.hideLoading()
                        await wx.showToast({
                            title: "创建行程失败",
                            icon: "none",
                            duration: 2000
                        })
                        return
                    }

                    const trip = await TripService.CreateTrip({
                        carId: this.carID,
                        start: loc,
                        avatar_url:this.data.avatarUrl
                    })

                    if (!trip.id) {
                        await wx.hideLoading()
                        await wx.showToast({
                            title: "创建行程失败",
                            icon: "none",
                            duration: 2000
                        })
                        return
                    }

                    this.carRefresher = setInterval(async ()=>{
                        const car = await CarService.getCar(this.carID)
                        if (car.status ===  UNLOCKED){
                            this.clearCarRefresher()
                            wx.redirectTo({
                                "url": `/pages/driving/driving?trip_id=${trip.id}`,
                                complete: () => {
                                    wx.hideLoading()
                                }
                            })
                        }
                    },2000)

                } catch (err) {
                    await wx.hideLoading()
                    await wx.showToast({
                        title: "开锁失败",
                        icon: "none",
                        duration: 2000
                    })
                    return
                }

            },
            fail: (res) => {
                console.log(res)
                wx.showToast({
                    icon: "none",
                    title: "请前往设置页授权位置信息"
                })
            }
        })
    },
    onUnload() {
        this.clearCarRefresher()
        wx.hideLoading()
    },
    clearCarRefresher(){
        if (this.carRefresher){
            clearInterval(this.carRefresher)
        }
    }
})